#include <REGX52.H>
#include "Timer0.h"
#include "Key.h"

// 0正序 1反序
unsigned char ledType = 0;

void main() {
	unsigned char key;
	
	Timer0_Init();
	P2 = 0xFE;
	
	while (1) {
		key = Key();
		if (key == 1) {
			ledType = 1;
		} else if (key == 4) {
			ledType = 0;
		}
	}

}

void Timer0_Isr(void) interrupt 1
{
	static unsigned int timer0Counter = 0;
	unsigned char tmp;
	
	timer0Counter++;
	TL0 = 0x66;				//设置定时初始值
	TH0 = 0xFC;				//设置定时初始值
	
	if (timer0Counter >= 500) {	// 0.5s
		timer0Counter = 0;
		if (ledType == 0) {
			tmp = ~P2 << 1;
			if (tmp == 0)
				tmp = 0x01;
		} else {
			tmp = ~P2 >> 1;
			if (tmp == 0)
				tmp = 0x80;
		}
		P2 = ~tmp;
	}			
}